package com.gitee.qdbp.jdbc.stream;

import org.springframework.jdbc.core.RowMapper;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.Paging;
import com.gitee.qdbp.jdbc.api.SqlDao;

/**
 * 分页+结果转换后续操作: 只有list()
 *
 * @author zhaohuihua
 * @version 20210530
 */
public class SqlOnPagingResultStream<T> {

    protected final SqlDao dao;
    protected final String sqlId;
    protected final Object params;
    protected final Paging paging;

    protected Class<T> resultType;
    protected RowMapper<T> rowMapper;

    SqlOnPagingResultStream(SqlDao dao, String sqlId, Object params, Paging paging, Class<T> resultType) {
        this.dao = dao;
        this.sqlId = sqlId;
        this.params = params;
        this.paging = paging;
        this.resultType = resultType;
    }

    SqlOnPagingResultStream(SqlDao dao, String sqlId, Object params, Paging paging, RowMapper<T> rowMapper) {
        this.dao = dao;
        this.sqlId = sqlId;
        this.params = params;
        this.paging = paging;
        this.rowMapper = rowMapper;
    }

    /**
     * 查询列表
     * 
     * @return 查询结果列表
     */
    public PageList<T> list() {
        if (resultType != null) {
            return this.dao.pageForObjects(sqlId, params, paging, resultType);
        } else {
            return this.dao.pageForObjects(sqlId, params, paging, rowMapper);
        }
    }

}
